require 'singleton'
require 'sharedance'

module ResultsetAutofilter

  if not Object.constants.include? "SESSION_ABSTRACT_HASH"
          SESSION_ABSTRACT_HASH = {:type => "session"}
  end           


  class Sessionabstract
    include Singleton    
    @@session_server = nil

    def initialize    
      case SESSION_ABSTRACT_HASH[:type]
      when 'sharedance'         
        @@session_server = Sharedance.new(SESSION_ABSTRACT_HASH)
      when 'session'                     
        @@session_server = Hash.new      
      end
    end

    def set_object(controller_object, index, data)             
      case SESSION_ABSTRACT_HASH[:type]
      when 'sharedance'
        begin
          @@session_server.store(:key => index.to_s, :data => Marshal.dump(data))
        rescue
          false
        end
      when 'session'     
        begin
          @@session_server[index] = controller_object
          @@session_server[index].session[index] = data
        rescue
          false
        end        
      end
    end


    def commit_object(controller_object, index, data)             
        @@session_server.store(:key => index.to_s, :data => Marshal.dump(data)) if SESSION_ABSTRACT_HASH[:type] == 'sharedance'
    end




    def get_object(index)
      case SESSION_ABSTRACT_HASH[:type]
      when 'sharedance'    
        begin
          o = @@session_server.fetch(:key => index.to_s)
          Marshal.load(o)            
        rescue
          false
        end
      when 'session'        
        begin
          @@session_server[index].session[index]
        rescue
          false
        end
      end
    end

    def get_value(controller_object, index, val)
      case SESSION_ABSTRACT_HASH[:type]
      when 'sharedance'    
        begin
          o = @@session_server.fetch(:key => index.to_s)
          gc = Marshal.load(o)            
          # You should use accessor or duck typing
          gc.send(val)
        rescue
          false
        end                
      when 'session'
        begin
          @@session_server[index].session[index].send(val)                      
        rescue
          false
        end                
      end        
    end


    def object?(controller_object, index)
      case SESSION_ABSTRACT_HASH[:type]
      when 'sharedance'  
        if @@session_server.fetch(:key => index.to_s)
          true
        else
          false
        end
      when 'session'
        if @@session_server[index].session[index]
          true
        else
          false
        end
      end
    end
    
   
    def delete(controller_object, index)
      case SESSION_ABSTRACT_HASH[:type]
      when 'sharedance'   
        begin
          @@session_server.delete(:key => index.to_s)        
        rescue
          false
        end                  
      when 'session'
        begin      
          @@session_server[index].session[index] = nil             
          @@session_server.delete(index)
        rescue
          false
        end                
      end        
    end

  end
end